﻿using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace Developer.Core.FrameWork.RegisterCenter.Service
{
    /// <summary>
    /// 依据项目启动时自动注册服务
    /// </summary>
    public class AutoRegisterServiceByHost : IHostedService,IDisposable
    {
        private readonly IServiceRegister serviceRegister;
        public AutoRegisterServiceByHost(IServiceRegister serviceRegister)
        {
            this.serviceRegister = serviceRegister;
        }

        public void Dispose()
        { 
        }

        public Task StartAsync(CancellationToken cancellationToken)
        {
            return Task.Run(() => this.serviceRegister.Register());
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            this.serviceRegister.Deregister();
            return Task.CompletedTask;
        }
    }
}
